Telegram Group & Telegram Channel
Пару фишек про шаблоны, которые могут спасти час дебага:

1. CTAD (Class Template Argument Deduction, C++17)
Не надо вручную указывать аргументы:


std::pair p(42, 3.14); // вместо std::pair<int, double> p(42, 3.14);
std::vector v = {1,2,3}; // компилятор сам выведет std::vector<int>


Помогает сократить код и избежать опечаток.

2. Fold-выражения (C++17) для арг-паков:


auto sum = [](auto... args){
return (args + ...); // ((a + b) + c) + ...
};
std::cout << sum(1,2,3,4); // 10


Позволяют писать операции над любым числом параметров без рекурсии.

3. SFINAE → Concepts (C++20)
Старый стиль через enable_if легко сломать:


template<class T>
std::enable_if_t<std::is_integral_v<T>, T>
foo(T x) { return x*2; }


С Concepts чище и понятнее:


template<std::integral T>
T foo(T x) { return x*2; }


4. CRTP (Static polymorphism)
Быстрее виртуалок и без RTTI:


template<class D>
struct Base {
void interface() { static_cast<D*>(this)->impl(); }
};
struct Derived : Base<Derived> {
void impl() { std::cout<<"OK\n"; }
};


Шаблоны — это не только про универсальность, но и про ясность кода. Освой тонкости, и они станут 🔧, а не головняком.

➡️ @cpp_geek



tg-me.com/cpp_geek/323
Create:
Last Update:

Пару фишек про шаблоны, которые могут спасти час дебага:

1. CTAD (Class Template Argument Deduction, C++17)
Не надо вручную указывать аргументы:


std::pair p(42, 3.14); // вместо std::pair<int, double> p(42, 3.14);
std::vector v = {1,2,3}; // компилятор сам выведет std::vector<int>


Помогает сократить код и избежать опечаток.

2. Fold-выражения (C++17) для арг-паков:


auto sum = [](auto... args){
return (args + ...); // ((a + b) + c) + ...
};
std::cout << sum(1,2,3,4); // 10


Позволяют писать операции над любым числом параметров без рекурсии.

3. SFINAE → Concepts (C++20)
Старый стиль через enable_if легко сломать:


template<class T>
std::enable_if_t<std::is_integral_v<T>, T>
foo(T x) { return x*2; }


С Concepts чище и понятнее:


template<std::integral T>
T foo(T x) { return x*2; }


4. CRTP (Static polymorphism)
Быстрее виртуалок и без RTTI:


template<class D>
struct Base {
void interface() { static_cast<D*>(this)->impl(); }
};
struct Derived : Base<Derived> {
void impl() { std::cout<<"OK\n"; }
};


Шаблоны — это не только про универсальность, но и про ясность кода. Освой тонкости, и они станут 🔧, а не головняком.

➡️ @cpp_geek

BY C++ geek


Warning: Undefined variable $i in /var/www/tg-me/post.php on line 283

Share with your friend now:
tg-me.com/cpp_geek/323

View MORE
Open in Telegram


C geek Telegram | DID YOU KNOW?

Date: |

The S&P 500 slumped 1.8% on Monday and Tuesday, thanks to China Evergrande, the Chinese property company that looks like it is ready to default on its more-than $300 billion in debt. Cries of the next Lehman Brothers—or maybe the next Silverado?—echoed through the canyons of Wall Street as investors prepared for the worst.

NEWS: Telegram supports Facetime video calls NOW!

Secure video calling is in high demand. As an alternative to Zoom, many people are using end-to-end encrypted apps such as WhatsApp, FaceTime or Signal to speak to friends and family face-to-face since coronavirus lockdowns started to take place across the world. There’s another option—secure communications app Telegram just added video calling to its feature set, available on both iOS and Android. The new feature is also super secure—like Signal and WhatsApp and unlike Zoom (yet), video calls will be end-to-end encrypted.

C geek from sg


Telegram C++ geek
FROM USA